#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging,urllib,urllib2
from google.appengine.api import urlfetch
'''
Created on 2010-7-15

@author: Fred
'''

FETION_API = 'http://sms.jkgc.net/send.php'

SEND_SUCCESS = 200
SEND_FAILTURE = 400

class TmpFetion():
    '''
    封装一个简单的飞信功能
    '''
    mobile_no = ''
    passwd = ''
    
    def __init__(self,mobile_no,passwd):
        self.mobile_no = mobile_no
        self.passwd = passwd
        
    def send_msg(self,send_to,content):
        form_fields = {
              "phone": self.mobile_no,
              "fetion_password": self.passwd,
              "tophone": send_to,
              "message": content.encode('utf8')
        }

        form_data = urllib.urlencode(form_fields)
#        urlfetch.fetch(url=FETION_API,payload=form_data,method=urlfetch.POST)

#        request_data = urllib.urlencode([('phone', self.mobile_no),('fetion_password',self.passwd),('tophone',send_to),('message',content.encode('utf-8'))])
#        logging.info('**%s**%s**%s**%s**'%(self.mobile_no,self.passwd,send_to,content))
        request = urllib2.Request(FETION_API,data=form_data)
        i = 1
        for i in range(3):
            try:
                logging.info('*************%d'%i)
#                print i
                res = urllib2.urlopen(request)
                return
            except:
                i = i+1
#        return res
#        logging.info('***********'+res.read())
        
if __name__=="__main__":
    tmp = TmpFetion('15958127591','sd145914sd1!')
    tmp.send_msg('15958127591', u'哈哈哈哈哈')